package com.dhh.sky.widget;

import java.util.ArrayList;
import java.util.HashMap;
import com.dhh.sky.R;
import com.dhh.sky.model.SkyDriveObject;
import com.dhh.sky.util.CUtil;
import com.dhh.sky.util.Constant;
import com.dhh.sky.util.FileManager;
import com.dhh.sky.util.MusicUtilities;
import com.dhh.sky.util.SongsManager;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaMetadataRetriever;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class SongListAdapter extends BaseAdapter {
	private Context context;
	private LayoutInflater inflater;
	private FileManager fm;
	private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
	private MediaMetadataRetriever mmr = new MediaMetadataRetriever();
	
	public SongListAdapter(Context context){
		super();
		this.context = context;
		fm = new FileManager(context);
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		songsList = new SongsManager().getPlayList();
	}

	@Override
	public int getCount() {
		return songsList.size();
	}

	@Override
	public HashMap<String,String> getItem(int position) {
		return songsList.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final View view = convertView != null ? convertView : inflater.inflate(R.layout.song_list_item,parent,false);
		ImageView coverView = (ImageView) view.findViewById(R.id.sl_cover);
		TextView titleView = (TextView) view.findViewById(R.id.sl_title);
		TextView artistView = (TextView) view.findViewById(R.id.sl_artist);
		TextView timeView = (TextView) view.findViewById(R.id.sl_time);
		
		HashMap<String,String> map = this.getItem(position);
		String title = map.get(Constant.SONG_TITLE);
		String path = map.get(Constant.SONG_PATH);
		mmr.setDataSource(path);
		String artist =  mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
		String time = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
		String songName = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);		
		
		if(songName!=null){
			titleView.setText(songName);
		}else{
			SkyDriveObject obj = fm.getFile(CUtil.getFilePreName(title));
			if(obj!=null){
				titleView.setText(CUtil.getFilePreName(obj.getName()));
			}else{
				titleView.setText(CUtil.getFilePreName(title));
			}
		}
		
		if(artist!=null){
			artistView.setText(artist);  
		}else{
			artistView.setText(context.getString(R.string.label_unknow_artist));
		}
		
		byte[] picBytes = mmr.getEmbeddedPicture();
		if(picBytes!=null && picBytes.length>0){
			Bitmap bitMap = BitmapFactory.decodeByteArray(mmr.getEmbeddedPicture(), 0, picBytes.length);
			coverView.setImageBitmap(bitMap);
		}else{
			coverView.setImageResource(R.drawable.audio);
		}
		
		if(time!=null && time.length()>0){
			timeView.setText(new MusicUtilities().milliSecondsToTimer(Long.valueOf(time)));
		}else{
			timeView.setText("");
		}
		
		return view;
	}

}
